package com.xiangxiao.rpan.storage.controller;

import com.xiangxiao.rpan.storage.constant.ResponseCode;
import com.xiangxiao.rpan.storage.constant.ResponseMessage;
import com.xiangxiao.rpan.storage.dto.JsonObject;
import com.xiangxiao.rpan.storage.entity.PasswordConfig;
import com.xiangxiao.rpan.storage.service.PasswordConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/11/22 14:44
 */
@RestController
@RequestMapping("/admin")
public class StorageSourcePasswordController {
  private static Logger logger = LoggerFactory.getLogger(StorageSourcePasswordController.class);

  @Resource
  private PasswordConfigService passwordConfigService;

  @GetMapping("/storage/{storageId}/password")
  public JsonObject getPasswordList(@PathVariable Integer storageId) {
    try {
      List<PasswordConfig> configs = passwordConfigService.findByStorageId(storageId);
      return new JsonObject(ResponseMessage.QUERY_PASSWORD_CONFIG_SUCCESS, ResponseCode.OK, configs);
    } catch (Exception e) {
      logger.error(ResponseMessage.QUERY_PASSWORD_CONFIG_FAILED, e);
      return new JsonObject(ResponseMessage.QUERY_PASSWORD_CONFIG_FAILED, ResponseCode.QUERY_PASSWORD_CONFIG_FAILED);
    }
  }

  @PostMapping("/storage/{storageId}/password")
  public JsonObject savePasswordList(@PathVariable Integer storageId, @RequestBody List<PasswordConfig> passwordList) {
    try {
      passwordConfigService.batchSave(storageId, passwordList);
      return new JsonObject(ResponseMessage.SAVE_PASSWORD_FLODER_SUCCESS, ResponseCode.OK, true);
    } catch (Exception e) {
      logger.error(ResponseMessage.SAVE_PASSWORD_FLODER_FAILED, e);
      return new JsonObject(ResponseMessage.SAVE_PASSWORD_FLODER_FAILED, ResponseCode.SAVE_PASSWORD_FLODER_FAILED);
    }
  }
}
